#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/http_request.h>
#include <iostream>
#include <stdlib.h>
#include <nlohmann/json.hpp>
#include <cstring>
#include "curl.cpp"

using namespace std;
using json=nlohmann::json;

class web : public cppcms::application {
public:
    web(cppcms::service &srv) :cppcms::application(srv)
    {
        // dispatcher().assign("/number/(\\d+)",&web::number,this,1);
        //    mapper().assign("number","/number/{1}");

        dispatcher().assign("/smile",&web::smile,this);
        mapper().assign("smile","/smile{1}");

        dispatcher().assign("/page",&web::page,this);
        mapper().assign("page","/page{1}");

        dispatcher().assign("",&web::welcome,this);
        mapper().assign("");

        dispatcher().assign("/index",&web::index,this);
        mapper().assign("/index");

        mapper().root("/web");
    }
    
    virtual void page();
    virtual void smile();
    virtual void welcome();
    virtual void index();
};

void web::index()
{
    const char* url="http://127.0.0.1/index.php";
    string sql="sql=select * from bill order by id asc limit 1,10";
    const char* post=sql.c_str();
    std::string result=curl(url,post);
    json jsonresult=json::parse(result);
    std::string to_result=jsonresult.dump(4);
    
    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << to_result;
}

void web::page()
{
    string pages= request().get("pages");
    const char* url="http://127.0.0.1/index.php";
    string sql="sql=select * from bill order by id asc limit "+pages+",10";
    
    const char* post=sql.c_str();
    std::string result=curl(url,post);

    json jsonresult=json::parse(result);

    std::string to_result=jsonresult.dump(4);

    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    
    response().out() << to_result;
}

void web::smile()
{

    const char* url="http://127.0.0.1/list.php?id=10";
   

    std::string result=curl2(url);
    json jsonresult=json::parse(result);
    std::string to_result=jsonresult.dump(4);
    
    response().set_header("Access-Control-Allow-Origin","*");
    response().set_header("Access-Control-Allow-Methods","*");
    response().set_header("Access-Control-Allow-Headers","*");
    response().set_header("Access-Control-Allow-Credentials", "true");
    response().content_type("application/json; charset=utf-8");
    response().out() << to_result;
}

void web::welcome()
{
    response().out() <<
        "<h1> Welcome To Page with links </h1>\n"
        "<a href='" << url("/number",1)  << "'>1</a><br>\n"
        "<a href='" << url("/number",15) << "'>15</a><br>\n"
        "<a href='" << url("/smile") << "' >:-)</a><br>\n";
    string t= request().get("t");
    std::cout << t <<endl;
}

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);

        srv.applications_pool().mount(
          cppcms::applications_factory<web>()
        );
        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr << e.what() << std::endl;
    }
}
